/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.db.jpa;

import com.db.jpa.exceptions.IllegalOrphanException;
import com.db.jpa.exceptions.NonexistentEntityException;
import db.Label;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import db.Labeltypes;
import db.Results;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author devashish
 */
public class LabelJpaController {

    public LabelJpaController() {
        emf = Persistence.createEntityManagerFactory("ExplorePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Label label) {
        if (label.getResultsCollection() == null) {
            label.setResultsCollection(new ArrayList<Results>());
        }
        if (label.getResultsCollection1() == null) {
            label.setResultsCollection1(new ArrayList<Results>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Labeltypes labelTypeId = label.getLabelTypeId();
            if (labelTypeId != null) {
                labelTypeId = em.getReference(labelTypeId.getClass(), labelTypeId.getLabelTypeId());
                label.setLabelTypeId(labelTypeId);
            }
            Labeltypes labelTypeId1 = label.getLabelTypeId1();
            if (labelTypeId1 != null) {
                labelTypeId1 = em.getReference(labelTypeId1.getClass(), labelTypeId1.getLabelTypeId());
                label.setLabelTypeId1(labelTypeId1);
            }
            Collection<Results> attachedResultsCollection = new ArrayList<Results>();
            for (Results resultsCollectionResultsToAttach : label.getResultsCollection()) {
                resultsCollectionResultsToAttach = em.getReference(resultsCollectionResultsToAttach.getClass(), resultsCollectionResultsToAttach.getResultsPK());
                attachedResultsCollection.add(resultsCollectionResultsToAttach);
            }
            label.setResultsCollection(attachedResultsCollection);
            Collection<Results> attachedResultsCollection1 = new ArrayList<Results>();
            for (Results resultsCollection1ResultsToAttach : label.getResultsCollection1()) {
                resultsCollection1ResultsToAttach = em.getReference(resultsCollection1ResultsToAttach.getClass(), resultsCollection1ResultsToAttach.getResultsPK());
                attachedResultsCollection1.add(resultsCollection1ResultsToAttach);
            }
            label.setResultsCollection1(attachedResultsCollection1);
            em.persist(label);
            if (labelTypeId != null) {
                labelTypeId.getLabelCollection().add(label);
                labelTypeId = em.merge(labelTypeId);
            }
            if (labelTypeId1 != null) {
                labelTypeId1.getLabelCollection().add(label);
                labelTypeId1 = em.merge(labelTypeId1);
            }
            for (Results resultsCollectionResults : label.getResultsCollection()) {
                Label oldLabelOfResultsCollectionResults = resultsCollectionResults.getLabel();
                resultsCollectionResults.setLabel(label);
                resultsCollectionResults = em.merge(resultsCollectionResults);
                if (oldLabelOfResultsCollectionResults != null) {
                    oldLabelOfResultsCollectionResults.getResultsCollection().remove(resultsCollectionResults);
                    oldLabelOfResultsCollectionResults = em.merge(oldLabelOfResultsCollectionResults);
                }
            }
            for (Results resultsCollection1Results : label.getResultsCollection1()) {
                Label oldLabel1OfResultsCollection1Results = resultsCollection1Results.getLabel1();
                resultsCollection1Results.setLabel1(label);
                resultsCollection1Results = em.merge(resultsCollection1Results);
                if (oldLabel1OfResultsCollection1Results != null) {
                    oldLabel1OfResultsCollection1Results.getResultsCollection1().remove(resultsCollection1Results);
                    oldLabel1OfResultsCollection1Results = em.merge(oldLabel1OfResultsCollection1Results);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Label label) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Label persistentLabel = em.find(Label.class, label.getLabelId());
            Labeltypes labelTypeIdOld = persistentLabel.getLabelTypeId();
            Labeltypes labelTypeIdNew = label.getLabelTypeId();
            Labeltypes labelTypeId1Old = persistentLabel.getLabelTypeId1();
            Labeltypes labelTypeId1New = label.getLabelTypeId1();
            Collection<Results> resultsCollectionOld = persistentLabel.getResultsCollection();
            Collection<Results> resultsCollectionNew = label.getResultsCollection();
            Collection<Results> resultsCollection1Old = persistentLabel.getResultsCollection1();
            Collection<Results> resultsCollection1New = label.getResultsCollection1();
            List<String> illegalOrphanMessages = null;
            for (Results resultsCollectionOldResults : resultsCollectionOld) {
                if (!resultsCollectionNew.contains(resultsCollectionOldResults)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Results " + resultsCollectionOldResults + " since its label field is not nullable.");
                }
            }
            for (Results resultsCollection1OldResults : resultsCollection1Old) {
                if (!resultsCollection1New.contains(resultsCollection1OldResults)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Results " + resultsCollection1OldResults + " since its label1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (labelTypeIdNew != null) {
                labelTypeIdNew = em.getReference(labelTypeIdNew.getClass(), labelTypeIdNew.getLabelTypeId());
                label.setLabelTypeId(labelTypeIdNew);
            }
            if (labelTypeId1New != null) {
                labelTypeId1New = em.getReference(labelTypeId1New.getClass(), labelTypeId1New.getLabelTypeId());
                label.setLabelTypeId1(labelTypeId1New);
            }
            Collection<Results> attachedResultsCollectionNew = new ArrayList<Results>();
            for (Results resultsCollectionNewResultsToAttach : resultsCollectionNew) {
                resultsCollectionNewResultsToAttach = em.getReference(resultsCollectionNewResultsToAttach.getClass(), resultsCollectionNewResultsToAttach.getResultsPK());
                attachedResultsCollectionNew.add(resultsCollectionNewResultsToAttach);
            }
            resultsCollectionNew = attachedResultsCollectionNew;
            label.setResultsCollection(resultsCollectionNew);
            Collection<Results> attachedResultsCollection1New = new ArrayList<Results>();
            for (Results resultsCollection1NewResultsToAttach : resultsCollection1New) {
                resultsCollection1NewResultsToAttach = em.getReference(resultsCollection1NewResultsToAttach.getClass(), resultsCollection1NewResultsToAttach.getResultsPK());
                attachedResultsCollection1New.add(resultsCollection1NewResultsToAttach);
            }
            resultsCollection1New = attachedResultsCollection1New;
            label.setResultsCollection1(resultsCollection1New);
            label = em.merge(label);
            if (labelTypeIdOld != null && !labelTypeIdOld.equals(labelTypeIdNew)) {
                labelTypeIdOld.getLabelCollection().remove(label);
                labelTypeIdOld = em.merge(labelTypeIdOld);
            }
            if (labelTypeIdNew != null && !labelTypeIdNew.equals(labelTypeIdOld)) {
                labelTypeIdNew.getLabelCollection().add(label);
                labelTypeIdNew = em.merge(labelTypeIdNew);
            }
            if (labelTypeId1Old != null && !labelTypeId1Old.equals(labelTypeId1New)) {
                labelTypeId1Old.getLabelCollection().remove(label);
                labelTypeId1Old = em.merge(labelTypeId1Old);
            }
            if (labelTypeId1New != null && !labelTypeId1New.equals(labelTypeId1Old)) {
                labelTypeId1New.getLabelCollection().add(label);
                labelTypeId1New = em.merge(labelTypeId1New);
            }
            for (Results resultsCollectionNewResults : resultsCollectionNew) {
                if (!resultsCollectionOld.contains(resultsCollectionNewResults)) {
                    Label oldLabelOfResultsCollectionNewResults = resultsCollectionNewResults.getLabel();
                    resultsCollectionNewResults.setLabel(label);
                    resultsCollectionNewResults = em.merge(resultsCollectionNewResults);
                    if (oldLabelOfResultsCollectionNewResults != null && !oldLabelOfResultsCollectionNewResults.equals(label)) {
                        oldLabelOfResultsCollectionNewResults.getResultsCollection().remove(resultsCollectionNewResults);
                        oldLabelOfResultsCollectionNewResults = em.merge(oldLabelOfResultsCollectionNewResults);
                    }
                }
            }
            for (Results resultsCollection1NewResults : resultsCollection1New) {
                if (!resultsCollection1Old.contains(resultsCollection1NewResults)) {
                    Label oldLabel1OfResultsCollection1NewResults = resultsCollection1NewResults.getLabel1();
                    resultsCollection1NewResults.setLabel1(label);
                    resultsCollection1NewResults = em.merge(resultsCollection1NewResults);
                    if (oldLabel1OfResultsCollection1NewResults != null && !oldLabel1OfResultsCollection1NewResults.equals(label)) {
                        oldLabel1OfResultsCollection1NewResults.getResultsCollection1().remove(resultsCollection1NewResults);
                        oldLabel1OfResultsCollection1NewResults = em.merge(oldLabel1OfResultsCollection1NewResults);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = label.getLabelId();
                if (findLabel(id) == null) {
                    throw new NonexistentEntityException("The label with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Label label;
            try {
                label = em.getReference(Label.class, id);
                label.getLabelId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The label with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Results> resultsCollectionOrphanCheck = label.getResultsCollection();
            for (Results resultsCollectionOrphanCheckResults : resultsCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Label (" + label + ") cannot be destroyed since the Results " + resultsCollectionOrphanCheckResults + " in its resultsCollection field has a non-nullable label field.");
            }
            Collection<Results> resultsCollection1OrphanCheck = label.getResultsCollection1();
            for (Results resultsCollection1OrphanCheckResults : resultsCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Label (" + label + ") cannot be destroyed since the Results " + resultsCollection1OrphanCheckResults + " in its resultsCollection1 field has a non-nullable label1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Labeltypes labelTypeId = label.getLabelTypeId();
            if (labelTypeId != null) {
                labelTypeId.getLabelCollection().remove(label);
                labelTypeId = em.merge(labelTypeId);
            }
            Labeltypes labelTypeId1 = label.getLabelTypeId1();
            if (labelTypeId1 != null) {
                labelTypeId1.getLabelCollection().remove(label);
                labelTypeId1 = em.merge(labelTypeId1);
            }
            em.remove(label);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Label> findLabelEntities() {
        return findLabelEntities(true, -1, -1);
    }

    public List<Label> findLabelEntities(int maxResults, int firstResult) {
        return findLabelEntities(false, maxResults, firstResult);
    }

    private List<Label> findLabelEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Label as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Label findLabel(Long id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Label.class, id);
        } finally {
            em.close();
        }
    }

    public int getLabelCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Label as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
